package com.meritdata.cloud.data.manager.access.controller;

import com.meritdata.cloud.data.base.ResultData;
import com.meritdata.cloud.data.manager.access.entity.TdDataSource;
import com.meritdata.cloud.data.manager.access.service.IDataSourceService;
import com.meritdata.cloud.resultmodel.ResultBody;
import com.meritdata.cloud.resultmodel.ResultStatus;
import com.meritdata.cloud.utils.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/api/manager/datasource")
@Api(value="数据源信息-DataSourceController",tags = "数据源信息")
public class DataSourceController {

	@Autowired
	private IDataSourceService dataSourceService;

	@Autowired
	private SessionUtils sessionUtils;

	@RequestMapping("/list")
	@ApiOperation(value = "数据源信息查询接口",notes = "获取所有的数据源信息列表")
	public ResultBody list() {
		try{
			List<TdDataSource> ds = dataSourceService.list();
			return ResultBody.success(ds);
		}catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}

	@RequestMapping("/get")
	@ApiOperation(value = "数据源信息查询接口",notes = "根据数据源id获取对应的数据源信息")
	public ResultBody getById(@ApiParam(value = "数据源id") @RequestParam Long id) {
		try{
			return ResultBody.success(dataSourceService.getById(id));
		}catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}

	@RequestMapping("/change-status")
	@ApiOperation(value = "修改数据源启用状态接口",notes = "修改数据源启用状态接口")
	public ResultBody changeDbStatusById(@ApiParam(value = "数据源启用状态(1:禁用)") @RequestParam int enable,
										 @ApiParam(value = "数据源id") @RequestParam Long id) {
		try{
			return ResultBody.success(dataSourceService.changeDbStatusById(enable,id));
		}catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}

	@RequestMapping("/delete")
	@ApiOperation(value = "数据源信息删除接口",notes = "根据数据源id删除对应的数据源信息")
	public ResultBody delete(@ApiParam(value = "数据源id") @RequestParam Long id) {
		try{
			return ResultBody.success(dataSourceService.delete(id));
		}catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}

	@RequestMapping("/create")
	@ApiOperation(value = "数据源信息新增接口",notes = "根据参数新增数据源信息")
	public ResultBody create(@ApiParam(value = "数据源名称") @RequestParam String name,
							 @ApiParam(value = "数据源描述",required=false) @RequestParam String description,
							 @ApiParam(value = "数据源连接字符串:URL") @RequestParam String url,
							 @ApiParam(value = "数据源登录用户名") @RequestParam String user,
							 @ApiParam(value = "数据源登录密码") @RequestParam String password,
							 @ApiParam(value = "数据源启用状态(1:禁用)",required=false) @RequestParam int enable,
							 @ApiParam(value = "数据源最大连接数",required=false) @RequestParam int maxActive,
							 @ApiParam(value = "数据源初始连接数",required=false) @RequestParam int initialSize,
							 @ApiParam(value = "数据源空闲回收时间",required=false) @RequestParam int maxWait,
							 @ApiParam(value = "数据源扩展属性(JSON)",required=false) @RequestParam String info,
							 @ApiParam(value = "数据源是否采集元数据(0:不开启，1:开启)")  @RequestParam int enableCollection ,
							 @ApiParam(value = "数据源类型") @RequestParam String type) {
		try{
			if(dataSourceService.nameExist(null,name)){
				ResultData rs = new ResultData();
				rs.setStatus(false);
				rs.setMsg("名称已存在");
				return ResultBody.success(rs);
			}
			return ResultBody.success(dataSourceService.create(name,description, url, user, password,enable,maxActive, initialSize, maxWait, info, enableCollection , type));
		}catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}

	@RequestMapping("/update")
	@ApiOperation(value = "数据源信息更新接口",notes = "根据参数更新数据源信息")
	public ResultBody update(@ApiParam(value = "数据源id") @RequestParam Long id,
							 @ApiParam(value = "数据源名称") @RequestParam String name,
							 @ApiParam(value = "数据源描述",required=false) @RequestParam String description,
							 @ApiParam(value = "数据源连接字符串:URL") @RequestParam String url,
							 @ApiParam(value = "数据源登录用户名") @RequestParam String user,
							 @ApiParam(value = "数据源登录密码") @RequestParam String password,
							 @ApiParam(value = "数据源启用状态(1:禁用)",required=false) @RequestParam int enable,
							 @ApiParam(value = "数据源最大连接数",required=false) @RequestParam int maxActive,
							 @ApiParam(value = "数据源初始连接数",required=false) @RequestParam int initialSize,
							 @ApiParam(value = "数据源空闲回收时间",required=false) @RequestParam int maxWait,
							 @ApiParam(value = "数据源扩展属性(JSON)",required=false) @RequestParam String info,
							 @ApiParam(value = "数据源是否采集元数据(0:不开启，1:开启)")  @RequestParam int enableCollection ,
							 @ApiParam(value = "数据源类型") @RequestParam String type) {
		try{
			if(dataSourceService.nameExist(id,name)){
				ResultData rs = new ResultData();
				rs.setStatus(false);
				rs.setMsg("数据源名称已存在");
				return ResultBody.success(rs);
			}
			return ResultBody.success(dataSourceService.update(id,name,description, url, user, password,enable,maxActive, initialSize, maxWait, info, enableCollection , type));
		}catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}
	@RequestMapping("/connection-test")
	@ApiOperation(value = "数据源信息删除接口",notes = "根据数据源id删除对应的数据源信息")
	public ResultBody connectionTest(@ApiParam(value = "数据源连接字符串:URL") @RequestParam String url,
									 @ApiParam(value = "数据源登录用户名") @RequestParam String user,
									 @ApiParam(value = "数据源登录密码") @RequestParam String password,
									 @ApiParam(value = "数据源扩展属性(JSON)",required=false) @RequestParam String info,
									 @ApiParam(value = "数据源类型") @RequestParam String type) {
		try{
			return ResultBody.success(dataSourceService.connectionTest(url,user,password,info,type));
		}catch (RuntimeException e) {
			return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
		}
	}

}
